/********************
点击考核名称后的指标填报弹窗
** */
<template>
  <el-dialog
    width="80%"
    title="审核指标"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="8vh"
    @close="closeDialog()"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      label-position="right"
      label-width="120px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="考核名称" prop="name" style="width:60%">
            <span>{{dataForm.name}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="周期名称" prop="date" style="width:60%">
            <span>{{dataForm.period}}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="考核时间" prop="sname" style="width:60%">
            <span>{{dataForm.assessBeginTime?dataForm.assessBeginTime.slice(0,10):""}}--{{dataForm.assessEndTime?dataForm.assessEndTime.slice(0,10):''}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="dataForm.type==0?'被考核人':'被考核机构'" style="width:60%;">
            <span v-if="dataForm.type==0">
              <span v-for="it in dataForm.assessTarget" :key="it">{{it}},</span>
            </span>
            <span v-else>
              <span v-for="it in dataForm.assessDeptName" :key="it">{{it}},</span>
            </span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="height: 10px; border-top: 1px solid #b0bde2"></div>

    <el-row>
      <el-table
        style="width: 100%;margin-top:20px"
        :data="dataList"
        border
        stripe
        header-align="center"
      >
        <el-table-column type="index" width="55" label="序号"></el-table-column>
        <el-table-column prop="type" align="center" label="指标类别" width="140"></el-table-column>
        <el-table-column prop="name" align="center" label="指标名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="weight" align="center" label="权重(%)">
          <template slot-scope="scope">
<!--            <el-form :model="scope.row" :rules="rules">
              <el-form-item prop="weight">
                <el-input v-model.number="scope.row.weight"></el-input>
              </el-form-item>
            </el-form>-->
            <el-input
              v-model="scope.row.weight"
              oninput="value=value.replace(/[^\d.]/g, '').replace(/^\./g, '').replace(/\.{2,}/g, '.').replace(/(\.\d+)\./g, '$1').replace(/^0+(\d)/, '$1').replace(/(\d{15})\d*/, '$1').replace(/(\.\d{2})\d*/, '$1')"
              placeholder="权重"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="formula" align="center" label="计算公式" show-overflow-tooltip></el-table-column>
        <el-table-column prop="targetValue" align="center" label="目标值" min-width="120">
          <template slot-scope="scope">
            <el-input v-model="scope.row.targetValue"
                      :rows="1"
                      type="textarea"></el-input>
          </template>

        </el-table-column>
        <el-table-column prop="unit" align="center" label="单位" show-overflow-tooltip></el-table-column>
        <el-table-column prop="completeValue" align="center" label="完成值获取方式" show-overflow-tooltip></el-table-column>
        <el-table-column
          prop="provideRealName"
          align="center"
          label="数据提供部门/人"
          show-overflow-tooltip
        ></el-table-column>
        <!--<el-table-column prop="itemIndex" align="center" label="排序号" show-overflow-tooltip></el-table-column>-->
        <el-table-column prop="state" align="center" label="状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{scope.row.state==0?'拟定中':'拟定完成'}}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" plain @click="handleConfirm">确定</el-button>
      <el-button type="primary" plain @click="closeDialog()">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  targetAllFill,
  targetDetail,
  saveFillTarget,
  indexExaminAllDataList
} from "@/api/dailyExamine/targetExamine/indicatorFormulation";
import { validatenull } from "@/util/validate"; //判断空
export default {
  data() {
    return {
      fatherData: {},
      dataList: [], //指标表格数据
      visible: false, //最外层弹窗显隐
      dataForm: {}, //表单对象
      /*rules:{
          weight: [
              { required: true,message: "请输入数字", trigger: "blur" },
              {type:"number",message:"权重必须为数字值"}],
      },*/
    };
  },
  created() {},
  methods: {
    // 查询数据详情
    getDataDetail(id) {
      targetDetail(id)
        .then(r => {
          if (r.data.code == 0) {
            this.dataForm = r.data.data;
            this.dataForm.assessTarget = r.data.data.participantList.map(it => {
              return it.assessRealName;
            });
            this.dataForm.assessDeptName = r.data.data.participantList.map(
              it => {
                return it.assessDeptName;
              }
            );
          } else {
            this.$message.error(r.data.msg);
          }
        })
        .catch(err => {});
    },
    // 查询需要自己填写的指标
    getNeedFillTarget(id) {
      indexExaminAllDataList(id)
        .then(r => {
          if (r.data.code == 0) {
            this.dataList = r.data.data;
          } else {
            this.$message.error(r.data.msg);
          }
        })
        .catch(err => {});
    },
    // 确定
    handleConfirm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          const szValid = /^[0-9]*$/;
          let flagWeight = this.dataList.every( it=> {
              return !validatenull(it.weight)
          }) ;
          if(!flagWeight){
              this.$message.error('权重不可为空');
              return ;
          }

            //验证是否超200字
            let flagTargetValue = this.dataList.every( it=> {
                return !validatenull(it.targetValue) && it.targetValue.length<=200 ;
            }) ;
            if (!flagTargetValue){
                this.$message.error('目标值不可为空且不可超过200字');
                return ;
            }

          saveFillTarget(this.dataList, this.fatherData.id, 1)
            .then(r => {
              if (r.data.code == 0) {
                this.$message.success("填报成功");
                this.closeDialog();
                this.$emit("closeDialog");
                this.$emit('refresh') ;
              }
            })
            .catch(err => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 初始化
    init(data) {
      this.visible = true;
      this.fatherData = data;

      this.getDataDetail(data.id);
      this.getNeedFillTarget(data.id);
    },
    // 表单提交
    dataFormSubmit() {},
    // 关闭
    closeDialog() {
      this.visible = false;
      this.$emit("closeDialog");
    }
  }
};
</script>

<style lang="scss">
</style>
